<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - Lua 5.1.5 - src/loslib.c</title>
  <link rel="stylesheet" type="text/css" href="../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../index.html">top level</a> - <a href="index.html">src</a> - loslib.c</td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">Lua 5.1.5</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">111</td>
            <td class="headerCovTableEntry">115</td>
            <td class="headerCovTableEntryHi">96.5 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2019-06-13 19:44:15</td>
            <td></td>
          </tr>
          <tr>
            <td class="headerItem">Legend:</td>
            <td class="headerValueLeg">            Lines:
            <span class="coverLegendCov">hit</span>
            <span class="coverLegendNoCov">not hit</span>
</td>
            <td></td>
          </tr>
          <tr><td><img src="../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<span class="lineNum">       1 </span>            : /*
<span class="lineNum">       2 </span>            : ** $Id: loslib.c,v 1.19.1.3 2008/01/18 16:38:18 roberto Exp $
<span class="lineNum">       3 </span>            : ** Standard Operating System library
<span class="lineNum">       4 </span>            : ** See Copyright Notice in lua.h
<span class="lineNum">       5 </span>            : */
<span class="lineNum">       6 </span>            : 
<span class="lineNum">       7 </span>            : 
<span class="lineNum">       8 </span>            : #include &lt;errno.h&gt;
<span class="lineNum">       9 </span>            : #include &lt;locale.h&gt;
<span class="lineNum">      10 </span>            : #include &lt;stdlib.h&gt;
<span class="lineNum">      11 </span>            : #include &lt;string.h&gt;
<span class="lineNum">      12 </span>            : #include &lt;time.h&gt;
<span class="lineNum">      13 </span>            : 
<span class="lineNum">      14 </span>            : #define loslib_c
<span class="lineNum">      15 </span>            : #define LUA_LIB
<span class="lineNum">      16 </span>            : 
<span class="lineNum">      17 </span>            : #include &quot;lua.h&quot;
<span class="lineNum">      18 </span>            : 
<span class="lineNum">      19 </span>            : #include &quot;lauxlib.h&quot;
<span class="lineNum">      20 </span>            : #include &quot;lualib.h&quot;
<span class="lineNum">      21 </span>            : 
<span class="lineNum">      22 </span>            : 
<span class="lineNum">      23 </span><span class="lineCov">         33 : static int os_pushresult (lua_State *L, int i, const char *filename) {</span>
<span class="lineNum">      24 </span><span class="lineCov">         33 :   int en = errno;  /* calls to Lua API may change this value */</span>
<span class="lineNum">      25 </span><span class="lineCov">         33 :   if (i) {</span>
<span class="lineNum">      26 </span><span class="lineCov">         28 :     lua_pushboolean(L, 1);</span>
<span class="lineNum">      27 </span><span class="lineCov">         28 :     return 1;</span>
<span class="lineNum">      28 </span>            :   }
<span class="lineNum">      29 </span>            :   else {
<span class="lineNum">      30 </span><span class="lineCov">          5 :     lua_pushnil(L);</span>
<span class="lineNum">      31 </span><span class="lineCov">          5 :     lua_pushfstring(L, &quot;%s: %s&quot;, filename, strerror(en));</span>
<span class="lineNum">      32 </span><span class="lineCov">          5 :     lua_pushinteger(L, en);</span>
<span class="lineNum">      33 </span><span class="lineCov">          5 :     return 3;</span>
<span class="lineNum">      34 </span>            :   }
<span class="lineNum">      35 </span>            : }
<span class="lineNum">      36 </span>            : 
<span class="lineNum">      37 </span>            : 
<span class="lineNum">      38 </span><span class="lineCov">          6 : static int os_execute (lua_State *L) {</span>
<span class="lineNum">      39 </span><span class="lineCov">          6 :   lua_pushinteger(L, system(luaL_optstring(L, 1, NULL)));</span>
<span class="lineNum">      40 </span><span class="lineCov">          6 :   return 1;</span>
<span class="lineNum">      41 </span>            : }
<span class="lineNum">      42 </span>            : 
<span class="lineNum">      43 </span>            : 
<span class="lineNum">      44 </span><span class="lineCov">         31 : static int os_remove (lua_State *L) {</span>
<span class="lineNum">      45 </span><span class="lineCov">         31 :   const char *filename = luaL_checkstring(L, 1);</span>
<span class="lineNum">      46 </span><span class="lineCov">         31 :   return os_pushresult(L, remove(filename) == 0, filename);</span>
<span class="lineNum">      47 </span>            : }
<span class="lineNum">      48 </span>            : 
<span class="lineNum">      49 </span>            : 
<span class="lineNum">      50 </span><span class="lineCov">          2 : static int os_rename (lua_State *L) {</span>
<span class="lineNum">      51 </span><span class="lineCov">          2 :   const char *fromname = luaL_checkstring(L, 1);</span>
<span class="lineNum">      52 </span><span class="lineCov">          2 :   const char *toname = luaL_checkstring(L, 2);</span>
<span class="lineNum">      53 </span><span class="lineCov">          2 :   return os_pushresult(L, rename(fromname, toname) == 0, fromname);</span>
<span class="lineNum">      54 </span>            : }
<span class="lineNum">      55 </span>            : 
<span class="lineNum">      56 </span>            : 
<span class="lineNum">      57 </span><span class="lineCov">          2 : static int os_tmpname (lua_State *L) {</span>
<span class="lineNum">      58 </span>            :   char buff[LUA_TMPNAMBUFSIZE];
<span class="lineNum">      59 </span>            :   int err;
<span class="lineNum">      60 </span><span class="lineCov">          2 :   lua_tmpnam(buff, err);</span>
<span class="lineNum">      61 </span><span class="lineCov">          2 :   if (err)</span>
<span class="lineNum">      62 </span><span class="lineNoCov">          0 :     return luaL_error(L, &quot;unable to generate a unique filename&quot;);</span>
<span class="lineNum">      63 </span><span class="lineCov">          2 :   lua_pushstring(L, buff);</span>
<span class="lineNum">      64 </span><span class="lineCov">          2 :   return 1;</span>
<span class="lineNum">      65 </span>            : }
<span class="lineNum">      66 </span>            : 
<span class="lineNum">      67 </span>            : 
<span class="lineNum">      68 </span><span class="lineCov">          2 : static int os_getenv (lua_State *L) {</span>
<span class="lineNum">      69 </span><span class="lineCov">          2 :   lua_pushstring(L, getenv(luaL_checkstring(L, 1)));  /* if NULL push nil */</span>
<span class="lineNum">      70 </span><span class="lineCov">          2 :   return 1;</span>
<span class="lineNum">      71 </span>            : }
<span class="lineNum">      72 </span>            : 
<span class="lineNum">      73 </span>            : 
<span class="lineNum">      74 </span><span class="lineCov">          2 : static int os_clock (lua_State *L) {</span>
<span class="lineNum">      75 </span><span class="lineCov">          2 :   lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);</span>
<span class="lineNum">      76 </span><span class="lineCov">          2 :   return 1;</span>
<span class="lineNum">      77 </span>            : }
<span class="lineNum">      78 </span>            : 
<span class="lineNum">      79 </span>            : 
<span class="lineNum">      80 </span>            : /*
<span class="lineNum">      81 </span>            : ** {======================================================
<span class="lineNum">      82 </span>            : ** Time/Date operations
<span class="lineNum">      83 </span>            : ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
<span class="lineNum">      84 </span>            : **   wday=%w+1, yday=%j, isdst=? }
<span class="lineNum">      85 </span>            : ** =======================================================
<span class="lineNum">      86 </span>            : */
<span class="lineNum">      87 </span>            : 
<span class="lineNum">      88 </span><span class="lineCov">          8 : static void setfield (lua_State *L, const char *key, int value) {</span>
<span class="lineNum">      89 </span><span class="lineCov">          8 :   lua_pushinteger(L, value);</span>
<span class="lineNum">      90 </span><span class="lineCov">          8 :   lua_setfield(L, -2, key);</span>
<span class="lineNum">      91 </span><span class="lineCov">          8 : }</span>
<span class="lineNum">      92 </span>            : 
<span class="lineNum">      93 </span><span class="lineCov">          1 : static void setboolfield (lua_State *L, const char *key, int value) {</span>
<span class="lineNum">      94 </span><span class="lineCov">          1 :   if (value &lt; 0)  /* undefined? */</span>
<span class="lineNum">      95 </span><span class="lineNoCov">          0 :     return;  /* does not set field */</span>
<span class="lineNum">      96 </span><span class="lineCov">          1 :   lua_pushboolean(L, value);</span>
<span class="lineNum">      97 </span><span class="lineCov">          1 :   lua_setfield(L, -2, key);</span>
<span class="lineNum">      98 </span>            : }
<span class="lineNum">      99 </span>            : 
<span class="lineNum">     100 </span><span class="lineCov">          2 : static int getboolfield (lua_State *L, const char *key) {</span>
<span class="lineNum">     101 </span>            :   int res;
<span class="lineNum">     102 </span><span class="lineCov">          2 :   lua_getfield(L, -1, key);</span>
<span class="lineNum">     103 </span><span class="lineCov">          2 :   res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);</span>
<span class="lineNum">     104 </span><span class="lineCov">          2 :   lua_pop(L, 1);</span>
<span class="lineNum">     105 </span><span class="lineCov">          2 :   return res;</span>
<span class="lineNum">     106 </span>            : }
<span class="lineNum">     107 </span>            : 
<span class="lineNum">     108 </span>            : 
<span class="lineNum">     109 </span><span class="lineCov">         25 : static int getfield (lua_State *L, const char *key, int d) {</span>
<span class="lineNum">     110 </span>            :   int res;
<span class="lineNum">     111 </span><span class="lineCov">         25 :   lua_getfield(L, -1, key);</span>
<span class="lineNum">     112 </span><span class="lineCov">         25 :   if (lua_isnumber(L, -1))</span>
<span class="lineNum">     113 </span><span class="lineCov">         13 :     res = (int)lua_tointeger(L, -1);</span>
<span class="lineNum">     114 </span>            :   else {
<span class="lineNum">     115 </span><span class="lineCov">         12 :     if (d &lt; 0)</span>
<span class="lineNum">     116 </span><span class="lineCov">          3 :       return luaL_error(L, &quot;field &quot; LUA_QS &quot; missing in date table&quot;, key);</span>
<span class="lineNum">     117 </span><span class="lineCov">          9 :     res = d;</span>
<span class="lineNum">     118 </span>            :   }
<span class="lineNum">     119 </span><span class="lineCov">         22 :   lua_pop(L, 1);</span>
<span class="lineNum">     120 </span><span class="lineCov">         22 :   return res;</span>
<span class="lineNum">     121 </span>            : }
<span class="lineNum">     122 </span>            : 
<span class="lineNum">     123 </span>            : 
<span class="lineNum">     124 </span><span class="lineCov">          5 : static int os_date (lua_State *L) {</span>
<span class="lineNum">     125 </span><span class="lineCov">          5 :   const char *s = luaL_optstring(L, 1, &quot;%c&quot;);</span>
<span class="lineNum">     126 </span><span class="lineCov">          5 :   time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL));</span>
<span class="lineNum">     127 </span>            :   struct tm *stm;
<span class="lineNum">     128 </span><span class="lineCov">          5 :   if (*s == '!') {  /* UTC? */</span>
<span class="lineNum">     129 </span><span class="lineCov">          2 :     stm = gmtime(&amp;t);</span>
<span class="lineNum">     130 </span><span class="lineCov">          2 :     s++;  /* skip `!' */</span>
<span class="lineNum">     131 </span>            :   }
<span class="lineNum">     132 </span>            :   else
<span class="lineNum">     133 </span><span class="lineCov">          3 :     stm = localtime(&amp;t);</span>
<span class="lineNum">     134 </span><span class="lineCov">          5 :   if (stm == NULL)  /* invalid date? */</span>
<span class="lineNum">     135 </span><span class="lineNoCov">          0 :     lua_pushnil(L);</span>
<span class="lineNum">     136 </span><span class="lineCov">          5 :   else if (strcmp(s, &quot;*t&quot;) == 0) {</span>
<span class="lineNum">     137 </span><span class="lineCov">          1 :     lua_createtable(L, 0, 9);  /* 9 = number of fields */</span>
<span class="lineNum">     138 </span><span class="lineCov">          1 :     setfield(L, &quot;sec&quot;, stm-&gt;tm_sec);</span>
<span class="lineNum">     139 </span><span class="lineCov">          1 :     setfield(L, &quot;min&quot;, stm-&gt;tm_min);</span>
<span class="lineNum">     140 </span><span class="lineCov">          1 :     setfield(L, &quot;hour&quot;, stm-&gt;tm_hour);</span>
<span class="lineNum">     141 </span><span class="lineCov">          1 :     setfield(L, &quot;day&quot;, stm-&gt;tm_mday);</span>
<span class="lineNum">     142 </span><span class="lineCov">          1 :     setfield(L, &quot;month&quot;, stm-&gt;tm_mon+1);</span>
<span class="lineNum">     143 </span><span class="lineCov">          1 :     setfield(L, &quot;year&quot;, stm-&gt;tm_year+1900);</span>
<span class="lineNum">     144 </span><span class="lineCov">          1 :     setfield(L, &quot;wday&quot;, stm-&gt;tm_wday+1);</span>
<span class="lineNum">     145 </span><span class="lineCov">          1 :     setfield(L, &quot;yday&quot;, stm-&gt;tm_yday+1);</span>
<span class="lineNum">     146 </span><span class="lineCov">          1 :     setboolfield(L, &quot;isdst&quot;, stm-&gt;tm_isdst);</span>
<span class="lineNum">     147 </span>            :   }
<span class="lineNum">     148 </span>            :   else {
<span class="lineNum">     149 </span>            :     char cc[3];
<span class="lineNum">     150 </span>            :     luaL_Buffer b;
<span class="lineNum">     151 </span><span class="lineCov">          4 :     cc[0] = '%'; cc[2] = '\0';</span>
<span class="lineNum">     152 </span><span class="lineCov">          4 :     luaL_buffinit(L, &amp;b);</span>
<span class="lineNum">     153 </span><span class="lineCov">         24 :     for (; *s; s++) {</span>
<span class="lineNum">     154 </span><span class="lineCov">         20 :       if (*s != '%' || *(s + 1) == '\0')  /* no conversion specifier? */</span>
<span class="lineNum">     155 </span><span class="lineCov">          9 :         luaL_addchar(&amp;b, *s);</span>
<span class="lineNum">     156 </span>            :       else {
<span class="lineNum">     157 </span>            :         size_t reslen;
<span class="lineNum">     158 </span>            :         char buff[200];  /* should be big enough for any conversion result */
<span class="lineNum">     159 </span><span class="lineCov">         11 :         cc[1] = *(++s);</span>
<span class="lineNum">     160 </span><span class="lineCov">         11 :         reslen = strftime(buff, sizeof(buff), cc, stm);</span>
<span class="lineNum">     161 </span><span class="lineCov">         11 :         luaL_addlstring(&amp;b, buff, reslen);</span>
<span class="lineNum">     162 </span>            :       }
<span class="lineNum">     163 </span>            :     }
<span class="lineNum">     164 </span><span class="lineCov">          4 :     luaL_pushresult(&amp;b);</span>
<span class="lineNum">     165 </span>            :   }
<span class="lineNum">     166 </span><span class="lineCov">          5 :   return 1;</span>
<span class="lineNum">     167 </span>            : }
<span class="lineNum">     168 </span>            : 
<span class="lineNum">     169 </span>            : 
<span class="lineNum">     170 </span><span class="lineCov">          7 : static int os_time (lua_State *L) {</span>
<span class="lineNum">     171 </span>            :   time_t t;
<span class="lineNum">     172 </span><span class="lineCov">          7 :   if (lua_isnoneornil(L, 1))  /* called without args? */</span>
<span class="lineNum">     173 </span><span class="lineCov">          2 :     t = time(NULL);  /* get current time */</span>
<span class="lineNum">     174 </span>            :   else {
<span class="lineNum">     175 </span>            :     struct tm ts;
<span class="lineNum">     176 </span><span class="lineCov">          5 :     luaL_checktype(L, 1, LUA_TTABLE);</span>
<span class="lineNum">     177 </span><span class="lineCov">          5 :     lua_settop(L, 1);  /* make sure table is at the top */</span>
<span class="lineNum">     178 </span><span class="lineCov">          5 :     ts.tm_sec = getfield(L, &quot;sec&quot;, 0);</span>
<span class="lineNum">     179 </span><span class="lineCov">          5 :     ts.tm_min = getfield(L, &quot;min&quot;, 0);</span>
<span class="lineNum">     180 </span><span class="lineCov">          5 :     ts.tm_hour = getfield(L, &quot;hour&quot;, 12);</span>
<span class="lineNum">     181 </span><span class="lineCov">          5 :     ts.tm_mday = getfield(L, &quot;day&quot;, -1);</span>
<span class="lineNum">     182 </span><span class="lineCov">          3 :     ts.tm_mon = getfield(L, &quot;month&quot;, -1) - 1;</span>
<span class="lineNum">     183 </span><span class="lineCov">          2 :     ts.tm_year = getfield(L, &quot;year&quot;, -1) - 1900;</span>
<span class="lineNum">     184 </span><span class="lineCov">          2 :     ts.tm_isdst = getboolfield(L, &quot;isdst&quot;);</span>
<span class="lineNum">     185 </span><span class="lineCov">          2 :     t = mktime(&amp;ts);</span>
<span class="lineNum">     186 </span>            :   }
<span class="lineNum">     187 </span><span class="lineCov">          4 :   if (t == (time_t)(-1))</span>
<span class="lineNum">     188 </span><span class="lineNoCov">          0 :     lua_pushnil(L);</span>
<span class="lineNum">     189 </span>            :   else
<span class="lineNum">     190 </span><span class="lineCov">          4 :     lua_pushnumber(L, (lua_Number)t);</span>
<span class="lineNum">     191 </span><span class="lineCov">          4 :   return 1;</span>
<span class="lineNum">     192 </span>            : }
<span class="lineNum">     193 </span>            : 
<span class="lineNum">     194 </span>            : 
<span class="lineNum">     195 </span><span class="lineCov">          1 : static int os_difftime (lua_State *L) {</span>
<span class="lineNum">     196 </span><span class="lineCov">          1 :   lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),</span>
<span class="lineNum">     197 </span><span class="lineCov">          1 :                              (time_t)(luaL_optnumber(L, 2, 0))));</span>
<span class="lineNum">     198 </span><span class="lineCov">          1 :   return 1;</span>
<span class="lineNum">     199 </span>            : }
<span class="lineNum">     200 </span>            : 
<span class="lineNum">     201 </span>            : /* }====================================================== */
<span class="lineNum">     202 </span>            : 
<span class="lineNum">     203 </span>            : 
<span class="lineNum">     204 </span><span class="lineCov">          3 : static int os_setlocale (lua_State *L) {</span>
<span class="lineNum">     205 </span>            :   static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
<span class="lineNum">     206 </span>            :                       LC_NUMERIC, LC_TIME};
<span class="lineNum">     207 </span>            :   static const char *const catnames[] = {&quot;all&quot;, &quot;collate&quot;, &quot;ctype&quot;, &quot;monetary&quot;,
<span class="lineNum">     208 </span>            :      &quot;numeric&quot;, &quot;time&quot;, NULL};
<span class="lineNum">     209 </span><span class="lineCov">          3 :   const char *l = luaL_optstring(L, 1, NULL);</span>
<span class="lineNum">     210 </span><span class="lineCov">          3 :   int op = luaL_checkoption(L, 2, &quot;all&quot;, catnames);</span>
<span class="lineNum">     211 </span><span class="lineCov">          3 :   lua_pushstring(L, setlocale(cat[op], l));</span>
<span class="lineNum">     212 </span><span class="lineCov">          3 :   return 1;</span>
<span class="lineNum">     213 </span>            : }
<span class="lineNum">     214 </span>            : 
<span class="lineNum">     215 </span>            : 
<span class="lineNum">     216 </span><span class="lineCov">          8 : static int os_exit (lua_State *L) {</span>
<span class="lineNum">     217 </span><span class="lineCov">          8 :   exit(luaL_optint(L, 1, EXIT_SUCCESS));</span>
<span class="lineNum">     218 </span>            : }
<span class="lineNum">     219 </span>            : 
<span class="lineNum">     220 </span>            : static const luaL_Reg syslib[] = {
<span class="lineNum">     221 </span>            :   {&quot;clock&quot;,     os_clock},
<span class="lineNum">     222 </span>            :   {&quot;date&quot;,      os_date},
<span class="lineNum">     223 </span>            :   {&quot;difftime&quot;,  os_difftime},
<span class="lineNum">     224 </span>            :   {&quot;execute&quot;,   os_execute},
<span class="lineNum">     225 </span>            :   {&quot;exit&quot;,      os_exit},
<span class="lineNum">     226 </span>            :   {&quot;getenv&quot;,    os_getenv},
<span class="lineNum">     227 </span>            :   {&quot;remove&quot;,    os_remove},
<span class="lineNum">     228 </span>            :   {&quot;rename&quot;,    os_rename},
<span class="lineNum">     229 </span>            :   {&quot;setlocale&quot;, os_setlocale},
<span class="lineNum">     230 </span>            :   {&quot;time&quot;,      os_time},
<span class="lineNum">     231 </span>            :   {&quot;tmpname&quot;,   os_tmpname},
<span class="lineNum">     232 </span>            :   {NULL, NULL}
<span class="lineNum">     233 </span>            : };
<span class="lineNum">     234 </span>            : 
<span class="lineNum">     235 </span>            : /* }====================================================== */
<span class="lineNum">     236 </span>            : 
<span class="lineNum">     237 </span>            : 
<span class="lineNum">     238 </span>            : 
<span class="lineNum">     239 </span><span class="lineCov">         87 : LUALIB_API int luaopen_os (lua_State *L) {</span>
<span class="lineNum">     240 </span><span class="lineCov">         87 :   luaL_register(L, LUA_OSLIBNAME, syslib);</span>
<span class="lineNum">     241 </span><span class="lineCov">         87 :   return 1;</span>
<span class="lineNum">     242 </span>            : }
<span class="lineNum">     243 </span>            : 
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.13</a></td></tr>
  </table>
  <br>

</body>
</html>
